﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CalculatorFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Label1_Click(object sender, EventArgs e)
        {
            bool b = CheckData(textBox1.Text, comboBox1.Text, textBox2.Text);
            if ( false==b)
            {
                return;
            }

            textBox3.Text = Calculate(textBox1.Text, comboBox1.Text, textBox2.Text).ToString();

        }
         
        public double Calculate(string number1, string ysf, string number2)
        {
            bool b = false;
            double n1 = GetNumber(number1, ref b);
            double n2 = GetNumber(number2, ref b);


            switch (ysf)
            {
                case "+":
                    return n1 + n2;
                case "-":
                    return n1 - n2;
                case "*":
                    return n1 * n2;
                case "/":
                    return n1 / n2;
                default:
                    break;
            }

            return 0;
        }

        /// <summary>
        /// 检查输入的数据
        /// </summary>
        /// <param name="number1">第一个数字</param>
        /// <param name="ysf">运算符</param>
        /// <param name="number2">第二个数字</param>
        /// <returns>数据是否合法，合法返回true，不合法返回false</returns>
        public bool CheckData(string number1,string ysf,string number2)
        {

            //检查输入的数据
            /*
             1.输入的字符是否是数字
             2.当运算符是/时，除数不能为0
             */
            if ("" == ysf)
            {
                MessageBox.Show("请选择运算符");
                return false;

            } 


            bool b = false;
            GetNumber(number1, ref b);
            if (false == b)
            {
                MessageBox.Show("第一个参数不是合法的数字");
                return false;
            }
            GetNumber(number2, ref b);
            if (false == b)
            {
                MessageBox.Show("第二个参数不是合法的数字");
                return false;
            }

            if ("/"==ysf && "0"==number2)
            {
                MessageBox.Show("除法运算时第二个参数不能是0");
                return false;
            }

            return true;
        }
        /// <summary>
        /// 检查字符串是否为合法的数字
        /// </summary>
        /// <param name="str">输入的字符串</param>
        /// <param name="b">返回值，是否合法</param>
        /// <returns>如果合法返回转换后的值</returns>
        public double GetNumber(string str,ref bool b)
        {
            double number = 0;
            if (double.TryParse(str, out number))
            {
                // Console.WriteLine("输入的字符串是一个合法的数字");
                b = true;
                return number;
            }
            else
            {
                b = false;
                return 0;
            }

        }

        private void Button18_Click(object sender, EventArgs e)
        {
            ysf = "";
            textBox4.Text = "";
            number1 = "";
        }


        private void NumberButtonEvent(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            DealNumber(button.Text);
           
        }


        public string number1;
        //public string number2;
        public string ysf = "";

        private void Button17_Click(object sender, EventArgs e)
        {
            //点击等号

            
            //检查数据
            bool b = CheckData(number1, ysf, textBox4.Text);
            if (false == b)
            {
                return;
            }

            textBox5.AppendText(Environment.NewLine + number1 + ysf + textBox4.Text);
            //执行计算
            textBox4.Text = Calculate(number1, ysf, textBox4.Text).ToString();
            textBox5.Text = textBox5.Text + "=" + textBox4.Text;
            n2Cls = false;
        }
 

        public bool n2Cls = false;
        public void DealNumber(string arg)
        {
            if (ysf=="")
            {
                DealTxt(arg);
            }
            else
            {
                if (false==n2Cls)
                {
                    textBox4.Text = "";
                    n2Cls = true;
                }

                DealTxt(arg); 
            }
             
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="arg"></param>
        public void DealTxt(string arg)
        {
            if ("+/-" == arg)
            {
                bool b = false;
                double n1 = GetNumber(textBox4.Text, ref b);

                textBox4.Text = ((-1) * n1).ToString();

            }
            else if ("." == arg)
            {
                if (textBox4.Text.Contains("."))
                {
                    return;
                }
                else
                {
                    textBox4.Text = textBox4.Text + arg;
                }
            }
            else
            {
                textBox4.Text = textBox4.Text + arg;
            }
        }

       

        private void Button10_Click(object sender, EventArgs e)
        {
            DealNumber("+/-");
        }
 

        private void Button12_Click(object sender, EventArgs e)
        {
            DealNumber(".");
        }

        private void Button13_Click(object sender, EventArgs e)
        {
            DealYsf("+");
        }

        public void DealYsf(string ysf1)
        {
            if (""==textBox4.Text)
            {
                return;
            }
            number1 = textBox4.Text;
            ysf = ysf1;
            n2Cls = false;
        }

        private void Button14_Click(object sender, EventArgs e)
        {
            DealYsf("-");
        }

        private void Button15_Click(object sender, EventArgs e)
        {
            DealYsf("*");
        }

        private void Button16_Click(object sender, EventArgs e)
        {
            DealYsf("/");
        }

        private void Button20_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox4.Text, out number);
            textBox4.Text = (number * number).ToString();
            n2Cls = false;

        }

        private void Button19_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox4.Text, out number);
            textBox4.Text = Math.Sqrt(number).ToString();
            n2Cls = false;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Button item in panel3.Controls)
            {
                item.Click += new System.EventHandler(NumberButtonEvent);

            }

        }
    }
}
